export const NameListTypes = ['黑名单', '白名单']

export const plugTypes = ['全局插件', '服务端插件', '客户端插件' ]

export const createGetItemFunc = <T>(data: T[], config?: { value?: string }) => {
  const { value = 'value' } = config || {}

  const obj = data.reduce((t, _) => {
    const key = _[value]
    return { ...t, [key]: _ }
  }, {})

  return (v: any): T => obj[v] || {}
}

// 标准状态枚举
enum NormalStatus { DISABLED = 0, ENABLED = 1 }

type StateType = 'info' | 'success' | 'warning' | 'danger'

type SelectItem = {
  label: string,
  value: any,
  type?: StateType
}

// 计量规则状态
export const ruleStatusSelect = (() => {
  const data: SelectItem[] = [
    { label: '启用', value: NormalStatus.ENABLED, type: 'success' },
    { label: '停用', value: NormalStatus.DISABLED, type: 'danger' }
  ]

  return { data, ...NormalStatus, getItem: createGetItemFunc(data) }
})()

// 流量控制策略状态
export const flowStatusSelect = (() => {
  const data: SelectItem[] = [
    { label: '启用', value: NormalStatus.ENABLED, type: 'success' },
    { label: '停用', value: NormalStatus.DISABLED, type: 'danger' }
  ]

  return { data, ...NormalStatus, getItem: createGetItemFunc(data) }
})()

// 黑白名单状态
export const nameListStatusSelect = (() => {
  const data: SelectItem[] = [
    { label: '启用', value: NormalStatus.ENABLED, type: 'success' },
    { label: '停用', value: NormalStatus.DISABLED, type: 'danger' }
  ]

  return { data, ...NormalStatus, getItem: createGetItemFunc(data) }
})()

// 黑白名单类型
export const nameListTypeSelect = (() => {
  enum Values { BLACK = 0, WHITE = 1 }
  const data: SelectItem[] = [
    { label: '黑名单', value: Values.BLACK },
    { label: '白名单', value: Values.WHITE }
  ]

  return { data, ...Values, getItem: createGetItemFunc(data) }
})()

// API状态
export const apiStatusSelect = (() => {
  enum Values { NO_TEST = 0, NO_PUBLISH = 1, PUBLISHED = 2, OFFLINE = 3 }
  const data: SelectItem[] = [
    { label: '未测试', value: Values.NO_TEST, type: 'info' },
    { label: '未发布', value: Values.NO_PUBLISH, type: 'warning' },
    { label: '已发布', value: Values.PUBLISHED, type: 'success' },
    { label: '已下线', value: Values.OFFLINE, type: 'danger' }
  ]

  return { data, ...Values, getItem: createGetItemFunc(data) }
})()

// 参数数据类型
export const paramDataTypeSelect = (() => {
  enum Values {
    STRING = 0,
    NUMBER = 1,
    ENUM = 2,
    OBJECT = 3,
    ARRAY = 4
  }

  const baseTypes: SelectItem[] = [
    { label: 'string', value: Values.STRING },
    { label: 'number', value: Values.NUMBER },
    { label: 'enum', value: Values.ENUM }
  ]

  const allTypes: SelectItem[] = [
    ...baseTypes,
    { label: 'object', value: Values.OBJECT },
    { label: 'array', value: Values.ARRAY }
  ]

  return { baseTypes, allTypes, ...Values, getItem: createGetItemFunc(allTypes) }
})()

// 协议
export const protocols = ['http', 'https']

// 对应入参来源
export const paramSourceSelect = (() => {
  enum Values { REQUEST = '0', CONST = '1', PLUGIN = '2', SYS_PARAM = '3' }
  const baseData: SelectItem[] = [
    { label: '前端请求', value: Values.REQUEST },
    { label: '系统参数', value: Values.SYS_PARAM }
  ]

  const allData: SelectItem[] = [
    ...baseData,
    { label: '鉴权插件', value: Values.PLUGIN }
  ]

  return { baseData, allData, ...Values, getItem: createGetItemFunc(allData) }
})()

// 协议
export const conditions = ['>', '<', '=', '!=']
